$(function () {
	//初始化表单
	var codeTemps = new Array();
	var str1 = "demo1";
	var rs = ["A","B","C","D","E","F","G","H"];
	var cs = ["1","2","3","4","5","6","7","8","9","0"];
	var row1 = "<tr><td></td>";
	for (var j = 0; j <cs.length ; j++) {
		row1 += "<td>"+ cs[j] +  "</td>";
	}
	row1 += "</tr>";
	str1 += row1;
	for (var i = 0; i <rs.length ; i++) {
		str1 += "<tr><td>" + rs[i] + "</td>";
		for (var j = 0; j <cs.length ; j++) {
			str1 += "<td class=\"code\">" +rs[i] + cs[j] +"</td>";
		}
		str1 += "</tr>";
	}

	$("#codeTable").html(str1);
	//alert(chrome.extension.getBackgroundPage())
	$("#moveOption").click(function(){
		chrome.tabs.create({url: 'options.html'});
	});
	
	$("#setCode").click(function(){
		var user = {};
		if(window.localStorage["userform"]){
			user = JSON.parse(window.localStorage["userform"]);
		}
		var code = {};
		if(window.localStorage["pwdform"]){
			var allcode = JSON.parse(window.localStorage["pwdform"]);
			for (var i = 0; i <codeTemps.length ; i++) {
				code["code" + (i+1)]=allcode[codeTemps[i]]||'';
			}
		}
		
		var obj = {action:"setLoginForm",params:{user:user,code:code}}
		//alert(JSON.stringify(obj));
		chrome.tabs.getSelected( null ,  function  (tab) { //获取当前tab
			 //向tab发送请求
			chrome.tabs.sendRequest(tab.id, obj,  function  (response) {
				 //alert(response.kw);
			});
		});
	});
	
	$(".code").click(function(){
		//alert($(this).html())
		//rndCode1 rndCode2 rndCode3
		if(codeTemps.length>=3){
			alert("最多选三个");
		}else{
			codeTemps.push($(this).html());
			$("#codeTemps").text(codeTemps.join("、"));
		}
	});
	$("#clearCode").click(function(){
		codeTemps = new Array();
		$("#codeTemps").text(codeTemps.join("、"));
	});
});
